﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace Framework_Tool
{
    class WordSearch
    {
        LearnerWordsearch lws;
        TakeAssessment takeAsmnt;

        public string resultString = "";

        // global variables
        private List<String> words = new List<String>();
        private int longestWord = 0;
        private Grid grid;
        private int numberOfLetters = 0; // number of correct letters available in wordsearch
        
        // constructor
        public WordSearch(List<String> words)
        {
            this.words = words;
        }

        // returns the completed grid
        public Grid drawWordSearch()
        {
            words.Sort((x, y) => x.Length.CompareTo(y.Length)); // sorts words by length
            grid = new Grid();
            grid.VerticalAlignment = VerticalAlignment.Top;
            grid.HorizontalAlignment = HorizontalAlignment.Left;
            Label[,] labelArray; // 2 dimensional array of labels (wordsearch letter)

            // loops through all words and sets longest to longestWord variable
            foreach(String s in words)
            {
                if (s.Length > longestWord)
                {
                    longestWord = s.Length;
                }
            }

            // initialisation of label array
            labelArray = new Label[(longestWord * words.Count) / 2, ((longestWord * words.Count) / 2)/2];
            
            //loops through 2D array, formats the grid and initialises labels
            for (int dx = 0; dx < (longestWord * words.Count) / 2; dx++)
            {
                ColumnDefinition c = new ColumnDefinition();
                c.Width = new GridLength(20);
                grid.ColumnDefinitions.Add(c);
                for (int dy = 0; dy < ((longestWord * words.Count) / 2)/2; dy++)
                {
                    RowDefinition r = new RowDefinition();
                    r.Height = new GridLength(20);
                    labelArray[dx, dy] = new Label();
                    grid.RowDefinitions.Add(r);
                    labelArray[dx, dy].FontWeight = FontWeights.Bold;
                    labelArray[dx, dy].FontSize = 14;
                    labelArray[dx, dy].Foreground = Brushes.White;
                    labelArray[dx, dy].Margin = new Thickness(0);
                    labelArray[dx, dy].Padding = new Thickness(0);
                    labelArray[dx, dy].Cursor = Cursors.Hand;
                    Grid.SetColumn(labelArray[dx, dy], dx);
                    Grid.SetRow(labelArray[dx, dy], dy);
                }
                
            }
            Random ran = new Random();
            // looping through each word to check if word can fit in current area available
            foreach (String w in words)
            {
                 // random number generator
                int direction = ran.Next(2); // 0 or 1
                restart: // placeholder to start again
                int x = -1;
                int y = -1;
                if (direction == 0)
                {
                    while (x < 0)
                        x = ran.Next(((longestWord * words.Count) / 2)) - w.Length;
                    while (y < 0)
                        y = ran.Next(((longestWord * words.Count) / 2) / 2);
                }
                if (direction == 1)
                {
                    while (x < 0)
                        x = ran.Next(((longestWord * words.Count) / 2));
                    while (y < 0)
                        y = ran.Next(((longestWord * words.Count) / 2) / 2) - w.Length;
                }
                int tempX = x;
                int tempY = y;
                for (int count = 0; count < w.Length + 1; count++)
                {
                    try
                    {
                        if (labelArray[tempX, tempY].Content != null) // if the label has content, find new place for word 
                            goto restart;
                        if (direction == 0)
                            tempX++; // place word horizontally
                        if (direction == 1)
                            tempY++; // place word vertically
                    }
                    catch (Exception)
                    {
                        goto restart; // if index goes out of bounds, find a new place for it
                    }
                }
                foreach (char c in w)
                {
                    numberOfLetters++; // all correct letters of words in wordsearch
                    labelArray[x, y].Content = c.ToString(); // sets content to label
                    labelArray[x, y].MouseUp += Search_MouseUp; // this is a correct charachetr click event
                    if (direction == 0)
                        x++;
                    if (direction == 1)
                        y++;
                }
                
            }
            // random number generator
            for (int dx = 0; dx < (longestWord * words.Count) / 2; dx++)
            {
                for (int dy = 0; dy < ((longestWord * words.Count) / 2)/2; dy++)
                {
                    if (labelArray[dx, dy].Content == null)
                    {
                        labelArray[dx, dy].MouseUp += Wrong_MouseUp; // incorrect click
                        labelArray[dx, dy].Content = (char)ran.Next(65, 91); // a - z in character codes, pick random
                    }
                    grid.Children.Add(labelArray[dx,dy]); // adds all labels to the grid
                }
            }

            return grid; // display grid
        }

        private void Wrong_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label l = (Label)sender; // object clicked is of type label
            l.Background = Brushes.Red; // set to red, if wrong
        }

        private void Search_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label l = (Label)sender; // object clicked is of type label
            l.Background = Brushes.Green; // set to red, if correct
        }

        public int done()
        {
            // initialise scores
            int numberWrong = 0;
            int numberRight = 0;

            foreach (Label l in grid.Children)
            {
                if (l.Background == Brushes.Red)
                    numberWrong++;
                if (l.Background == Brushes.Green)
                    numberRight++;
               
            }
            if (numberRight != numberOfLetters)
            {
                MessageBox.Show("You have not found all the words yet. \n Keep Trying!");
                return 0;
            }
            else
            {
                double logic = (((double)numberOfLetters - (double)numberWrong) / (double)numberOfLetters);
                double result = (logic * 100);
                result = Math.Round(result, 2);
                resultString = Convert.ToString(result);
                return 1;
            }
        }
    }
}
